import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_comm_base_framework/core/CommBasePage.dart';
import 'package:flutter_comm_base_framework/core/utils/ColorsUtil.dart';
import 'package:flutter_comm_base_framework/core/utils/dialog_utils.dart';
import 'package:flutter_comm_base_framework/core/utils/style_util.dart';
import 'package:flutter_comm_base_framework/core/view/my_appbar.dart';
import 'package:flutter_comm_base_framework/core/widgets/custom_button.dart';
import 'package:flutter_comm_base_framework/core/widgets/custom_text.dart';
import 'package:flutter_comm_base_framework/core/widgets/custom_view_panel.dart';
import 'package:flutter_comm_base_framework/flutter_comm_base_framework.dart';
import 'package:flutter_comm_base_framework/core/date_picker/date_picker.dart';
import 'package:flutter_comm_base_framework/core/date_picker/index.dart';
import 'package:flutter_comm_base_framework/core/date_picker/date.dart';

class TestPage extends CommBasePage{

  @override
  CommBaseState getPageState() {
    return TestPageSate();
  }
}
class TestPageSate extends CommBaseState<TestPage>{

  String _platformVersion = 'Unknown';
  TextEditingController _txtController = new TextEditingController();

  @override
  void initBaseParams() {
    initPlatformState();
  }

  @override
  void initPageData() {

  }

  @override
  bool hasKeyboardCtrl() {
    return true;
  }

  @override
  Widget getViewsWidget() {
    return Scaffold(
        appBar: MyAppBar(
          title: Text('Flutter基础框架',style: TextStyle(fontSize: 18,color: ColorsUtil.black_333),),
        ),
        body: Container(
          color: ColorsUtil.view_background,
          child: Column(
            children: [
              CustomText('系统版本: $_platformVersion',
                padding: EdgeInsets.all(10),
                alignment: Alignment.center,
                textSize: 12,
              ),
              Container(
                color: Colors.white,
                padding: EdgeInsets.all(10),
                child: Column(
                  children: [
                    Row(
                      crossAxisAlignment: CrossAxisAlignment.end,
                      children: [
                        CustomText("左侧提示提示",
                          width: 70,
                          margin: EdgeInsets.only(right: 15),
                        ),
                        Expanded(flex: 1,
                            child: CustomViewPanel.buildViewByHeight(TextField(
                              controller: _txtController,
                              textAlign: TextAlign.right,
                              keyboardType: TextInputType.number,
                              inputFormatters: [
                                WhitelistingTextInputFormatter(RegExp("[0-9]")),
                                LengthLimitingTextInputFormatter(8)
                              ],
                              decoration: InputDecoration(
                                  enabledBorder: StyleUtil.input_line_grey,
                                  contentPadding: EdgeInsets.only(left: 5,right: 5,bottom: 15)
                              ),
                            ), 30)
                        ),
                        CustomText("T"),
                      ],
                    ),
                    CustomText("第二行测试内容",
                      margin: EdgeInsets.only(top: 15),
                    ),
                    GestureDetector(
                      onTap: toSelDateTimeClick,
                      child: CustomText("点击打开日期事件选择框",
                        margin: EdgeInsets.only(top: 15),),
                    ),
                  ],
                ),
              ),
              CustomButton("确认提交", toSubmitClick,margin: EdgeInsets.all(20),),
            ],
          ),
        )
    );
  }

  void toSubmitClick(){
    String msg = _txtController.text;
    DialogUtil().showAlertDialog(context, "获取到内容【$msg】", "确定", "取消", (flag){

    });
  }

  void toSelDateTimeClick(){
    MyDatePickerUtil.showPicker(
      context: context,
      current: dateStr,
      mode: MyPickerMode.date,
      onConfirm: _setDate('yyyy-MM-dd',1), //yyyy-MM-dd HH:mm:ss
    );
  }

  String dateStr;
  _setDate(String formatStr,int type){
    return (_date) {
      setState(() {
        dateStr = MyDate.format(formatStr, _date);
      });
      showToastTip("类型$type选择的日期$dateStr");
    };
  }

  Future<void> initPlatformState() async {
    String platformVersion;
    try {
      platformVersion = await FlutterCommBaseFramework.platformVersion;
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
    });
  }
}